<?php
    class Profile {
        // ATRIBUT
        public $database;
        public $session;
        public $view_profile;

        // METHOD
        // konstruktor
        public function __construct($database,$session,$id) {
            $this->database = $database;
            $this->session = $session;
            $this->view_profile = $id;
        }

        // menggenerate header sesuai role
        public function genHeader() {
            if ($this->session->id_role==1) {
                include 'layout/general_layout/user_logged_header.php';
                include 'layout/general_layout/menu_admin.php';
            }
            else if ($this->session->id_role==2) {
                include 'layout/general_layout/user_logged_header.php';
                include 'layout/general_layout/menu_user.php';
            }
            else {
                include 'layout/general_layout/register_login_header.php';
                include 'layout/general_layout/description_bar.php';
            }
        }

        // menggenerate kontent sesuai referensi halaman
        public function genContent($content) {
            //include 'ajax/gen_content.php';
            if ($content=='wall' || $content=='message' || $content=='photo' ||$content=='null') {
                echo "<script type='text/javascript'>\n";
                echo "var a = createXMLHttpRequestObject();\n";
                if ($content==null) {
                    echo "sendGetRequestToServerAndResponse(a,'ajax/gen_content.php','ajax_wrapper', false);\n";
                }
                else if ($content=='message') {
                    echo "sendGetRequestToServerAndResponse(a,'ajax/gen_content.php?page=message&n=".$_GET['n']."','ajax_wrapper', false);\n";
                }
                else {
                    echo "sendGetRequestToServerAndResponse(a,'ajax/gen_content.php?id=".$this->view_profile['username']."&page=".$content."','ajax_wrapper', false);\n";
                }
                echo "</script>";
            }
            else {
                if ($content==null) {
                    include 'layout/profile_layout/profile_info_user.php';
                    include 'layout/profile_layout/profile_follow.php';
                }
                else if ($content=="wall") {
                    include 'layout/profile_layout/profile_wall.php';
                }
                else if ($content=="message") {
                    if ($this->session->isLoggedIn) {
                        include 'layout/profile_layout/profile_message_home.php';
                    }
                    else {
                        header("location:index.php");
                        die();
                    }
                }
                else if ($content=="photo") {
                    include 'layout/profile_layout/profile_photo_list.php';
                }
                else if ($content=="photo-upload") {
                    if ($this->session->isLoggedIn) {
                        include 'layout/profile_layout/profile_photo_upload.php';
                    }
                    else {
                        header("location:index.php");
                        die();
                    }
                }
                else if ($content=="photo-detail") {
                    include 'layout/profile_layout/profile_photo_desc.php';
                }
                else if ($content=="photo-edit") {
                    include 'layout/profile_layout/profile_photo_edit.php';
                }
                else if ($content=="edit-profile") {
                    if ($this->session->isLoggedIn) {
                        include 'layout/profile_layout/profile_edit_profile.php';
                    }
                    else {
                        header("location:index.php");
                        die();
                    }
                }
                else if ($content=="new-message") {
                    if ($this->session->isLoggedIn) {
                        include 'layout/profile_layout/profile_message_new.php';
                    }
                    else {
                        header("location:index.php");
                        die();
                    }
                }
                else if ($content=="show-message") {
                    if ($this->session->isLoggedIn) {
                        include 'layout/profile_layout/profile_message_read.php';
                    }
                    else {
                        header("location:index.php");
                        die();
                    }
                }
            }

        }

        public function genContentAJAX($content) {
            if ($content=='wall') {
                include '../layout/profile_layout/profile_wall.php';
            }
            else if ($content=="message") {
                if ($this->session->isLoggedIn) {
                    include '../layout/profile_layout/profile_message_home.php';
                }
                else {
                    header("location:index.php");
                    die();
                }
            }
            else if ($content=="photo") {
                include '../layout/profile_layout/profile_photo_list.php';
            }
            else if ($content=="profile") {
                include '../layout/profile_layout/profile_info_user.php';
                include '../layout/profile_layout/profile_follow.php';
            }
            else if ($content=="photo-detail") {
                    include '../layout/profile_layout/profile_photo_desc.php';
            }
        }

        public function genWall($user) {
            include '../process/wall_generator.php';
        }

        public function isFollowing() {
            $query="SELECT * FROM ".$this->database->t_follow." WHERE username='".$this->session->username."' and follow='".$this->view_profile['username']."'";
            $result = $this->database->execQuery($query);
            $count = mysql_num_rows($result);
            if($count==1){
                return true;
            }
            else {
                return false;
            }
        }

        public function isShow() {
            if ((($this->view_profile['wall_role'] == 1) ||
                    ($this->view_profile['wall_role'] == 2 && ($this->isFollowing() || $this->view_profile['username'] == $this->session->username)) ||
                    ($this->view_profile['wall_role'] == 3 && $this->view_profile['username'] == $this->session->username))
                    && $this->session->isLoggedIn ) {
                return true;
            } else {
                return false;
            }
        }

        public function isOwnProfile() {
            if ($this->session->activeUser==$this->view_profile && $this->session->isLoggedIn) {
                return true;
            }
            else {
                return false;
            }
        }

        public function genTag($id){
            if (!isset($_GET['m'])) {
                include "process/generate_tag.php";
            } else {
                include "../process/generate_tag.php";
            }
        }
    }
?>
